package com.baoyi.landscape;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import net.youmi.android.AdManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.baoyi.adapter.GridAdapter;
import com.baoyi.content.content;
import com.baoyi.doamin.Comments;
import com.baoyi.doamin.GridInfo;
import com.baoyi.doamin.WorkItem;
import com.baoyi.service.CommentsServiceImpl;
import com.baoyi.service.WorkItemServiceImpl;
import com.baoyi.widget.RemoteImageView;

public class WorkViewUI extends Activity {
	/** Called when the activity is first created. */

	private WorkItem workItem;
	private RemoteImageView remoteImageView;
	static {
		AdManager.init(content.id, content.pwd, content.time, content.con,
				content.banben);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.workview);
		gridview = (GridView) findViewById(R.id.gridview);
		List<GridInfo> list = new ArrayList<GridInfo>();
		list.add(new GridInfo("上一副"));
		list.add(new GridInfo("我要评论"));
		list.add(new GridInfo("下一副"));
		// workItem=
		Object oo = getIntent().getExtras().get("it");
		if (oo != null) {
			workItem = (WorkItem) oo;
			getIntent().getExtras().remove("it");
		}

		Integer id = (Integer) getIntent().getExtras().get("id");
		if (null != id) {
			Log.d(BaoyiApplication.TAG, "下一幅或者上一幅");
			WorkItemServiceImpl serviceImpl = new WorkItemServiceImpl();
			workItem = serviceImpl.findbyid(id);
		}
		if (null == workItem) {
			Toast.makeText(this, "作品不存在", Toast.LENGTH_SHORT);
			return;
			// Intent item = new Intent(WorkViewUI.this, WorkListUI.class);
			// startActivity(item);
		}
		remoteImageView = (RemoteImageView) findViewById(R.id.imageView1);
		String url = content.server + workItem.getBigimageurl();
		remoteImageView.setImageUrl(url);
		remoteImageView.setDefaultImage(R.drawable.fd);
		adapter = new GridAdapter(this);
		adapter.setList(list);
		gridview.setAdapter(adapter);
		gridview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Log.d(BaoyiApplication.TAG, arg2 + "");
				if (arg2 == 0) {
					Intent item = new Intent(WorkViewUI.this, WorkViewUI.class);
					item.putExtra("id", workItem.getUp());
					startActivity(item);
					workItem = null;
					finish();
				}
				if (arg2 == 1) {
					/*
					 * Dialog dialog = new Dialog(WorkViewUI.this);
					 * dialog.setContentView(R.layout.discussdailog);
					 * 
					 * dialog.setTitle("我要评论"); dialog.show();
					 */
					showDialog(1);
				}
				if (arg2 == 2) {
					Intent item = new Intent(WorkViewUI.this, WorkViewUI.class);
					item.putExtra("id", workItem.getPre());
					startActivity(item);
					workItem = null;
					finish();
				}

			}
		});

		listView = (ListView) findViewById(R.id.listView1);
		/*
		 * CommentsAdapter adapter = new CommentsAdapter(this, 12);
		 * listView.setAdapter(adapter);
		 */

		if (null != workItem) {
			CommentsServiceImpl service = new CommentsServiceImpl();
			List<Comments> ls = service.findbypid(workItem.getId());
			SimpleAdapter simpleAdapter = buildSimpleAdapter(ls);
			listView.setAdapter(simpleAdapter);
			listView.setDividerHeight(0);
		}

	}

	private SimpleAdapter buildSimpleAdapter(List dataBean) {
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		for (Iterator iterator = dataBean.iterator(); iterator.hasNext();) {
			Comments Info = (Comments) iterator.next();

			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("content", Info.getContents());
			map.put("time", Info.getPubtime());
			list.add(map);
		}
		SimpleAdapter simpleAdapter = new SimpleAdapter(WorkViewUI.this, list,
				R.layout.work_item, new String[] { "content", "time" },
				new int[] { R.id.workcontent, R.id.worktimeid });
		return simpleAdapter;
	}

	private Dialog buildDialog1(Context context) {
		LayoutInflater inflater = LayoutInflater.from(this);
		final View textEntryView = inflater.inflate(R.layout.discussdailog,
				null);
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("我要评论");
		builder.setView(textEntryView);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				EditText content = (EditText) textEntryView
						.findViewById(R.id.editText1);

				String ss = content.getText().toString();
				ss = ss.trim();
				if (ss != null && ss.length() > 2) {
					Comments commentsitem = new Comments();
					commentsitem.setContents(ss);
					commentsitem.setId(1);
					commentsitem.setIp("");
					commentsitem.setPubtime("");
					commentsitem.setWorkitemid(workItem.getId());
					CommentsServiceImpl commentsServiceImpl = new CommentsServiceImpl();
					commentsServiceImpl.add(commentsitem);
					Intent item = new Intent(WorkViewUI.this, WorkViewUI.class);
					item.putExtra("id", workItem.getId());
					startActivity(item);
					Toast.makeText(WorkViewUI.this, "添加评论成功",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(WorkViewUI.this, "请填写评论内容！",
							Toast.LENGTH_SHORT).show();
				}

			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {

			}
		});
		return builder.create();
	}

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case 1:
			return buildDialog1(WorkViewUI.this);
		}
		return null;
	}

	private ListView listView;
	private WebView webView;
	private Button button;
	private GridView gridview;
	private GridAdapter adapter;
}
